import MySQLdb,os,sys,csv
import MySQLdb.cursors
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
path=os.getcwd()+'/'
origin_file=path+'course.csv'

input_file={'error':path+'error.txt','right':path+'right.txt'}
course_file=open(origin_file, 'rb')

reader=csv.reader(course_file)
rows=[];
for row in reader:
    rows.append(row)
course_file.close()
print 'ee'

conn = MySQLdb.connect("54json.com","slave","slave","student",charset='utf8',cursorclass = MySQLdb.cursors.DictCursor)

cursor = conn.cursor()

for row in rows:
    tt=row[0].decode('gbk')
    if tt:
       cursor.execute ("select * from student where name='"+tt+"' limit 4")
       count=cursor.rowcount
       count=int(count)
       if count==1:
          pass
          data=cursor.fetchone()
          print tt+'--'+str(count)
          print_out(data)
       elif count>=2:
          pass
          datas=cursor.fetchall()
          print tt+'--'+str(count)
          for data in datas:
              print_out(data)
cursor.close ()
conn.close ()
print 'ok'


'''
conn = MySQLdb.connect("54json.com","slave","slave","student",charset='utf8')

cursor = conn.cursor()
cursor.execute ("select * from student limit 4")

res = cursor.fetchall()


f=open(path,'a')
for row in res:
    f.write('ee')
    print(unicode(row))
f.close()
cursor.close ()
conn.close ()
'''
def print_out(data):
    temp_str=str(data['student_intention_id'])+'--'+str(data['student_id'])+'--'+str(data['phone'])+'--'+data['name']+'--'+str(data['remaining_time'])
    print temp_str
    temp_str=str(data['student_intention_id'])+'--'+str(data['student_id'])+'--'+str(data['phone'])+'--'+data['name']+'--'+str(data['remaining_time'])+"\r\n"
    write_file(temp_str)

def write_file(str):
    f=open(os.getcwd()+'/right.txt','a',)
    f.write(str)
    f.close()
